package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.OrderService;
import com.sky.service.ReportService;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private SetDishMapper setDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private OrderMapper orderMapper;
    /**
     * 营业数据
     * @return
     */
    @Override
    public BusinessDataVO BusinessData() {
        //获取今天最早开始时间和最晚结束时间
        LocalDateTime begin = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime end = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        map.put("status", Orders.COMPLETED);
        Double turnover = reportMapper.getTurnover(map);//营业额
        Integer validOrderCount = reportMapper.getOrderCount(map);//有效订单数
        Integer newUsers = reportMapper.getUserSta(map);//新增用户数
        Double unitPrice = reportMapper.getAvgPrice(map);//平均单价
        map.put("status",null);
        Integer orderCount = reportMapper.getOrderCount(map);//订单总数
        Double orderCompletionRate = 0.0;
        if(orderCount != 0){
            orderCompletionRate = validOrderCount.doubleValue() / orderCount;
        }
        return BusinessDataVO.builder().turnover(turnover).validOrderCount(validOrderCount).
                newUsers(newUsers).unitPrice(unitPrice).
                orderCompletionRate(orderCompletionRate).build();
    }
    /**
     * 套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Integer sold = setDishMapper.countByStatus(StatusConstant.ENABLE);//统计正在发售的套餐数量
        Integer discontinued = setDishMapper.countByStatus(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder().sold(sold).discontinued(discontinued).build();
    }
    /**
     * 菜品总览
     */
    @Override
    public DishOverViewVO getDishOverView() {
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE);
        Integer discontinued = dishMapper.countByStatus(StatusConstant.DISABLE);
        return DishOverViewVO.builder().sold(sold).discontinued(discontinued).build();
    }

    /**
     * 订单总览
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        LocalDateTime begin = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime end = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        map.put("status", Orders.COMPLETED);
        Integer completeOrders = orderMapper.getCountDay(map);
        map.put("status", Orders.CANCELLED);
        Integer cancelOrders = orderMapper.getCountDay(map);
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.getCountDay(map);
        map.put("status", Orders.CONFIRMED);
        Integer deliveryOrders = orderMapper.getCountDay(map);
        map.put("status", null);
        Integer allOrders = orderMapper.getCountDay(map);
        return OrderOverViewVO.builder()
                .cancelledOrders(cancelOrders)
                .completedOrders(completeOrders)
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveryOrders)
                .allOrders(allOrders)
                .build();
    }

}
